// pages/baobei/baobei.js
const app=getApp();
import WxParse from '../../wxParse/wxParse.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: '',
    array: ['aaaa', 'bbb', 'ccc', 'ddd'],
    index: '',
    fotnum: 1,
    // 三联属性
    array: ['省'],
    shiarray: ['市'],
    quarray: ['区'],
    shengindex: 0,
    shiindex: 0,
    quindex: 0,
    hiddenAlert:true,
    area:[
      

    ]
  },
  bindDateChange: function(e) {
    console.log(e);
    this.setData({
      region: e.detail.value
    })
    this.gethouse(e.detail.code[2])
  },
  //楼盘选择
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value); //index为数组点击确定后选择的item索引
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.kanfang_xieyi();
    this.house_area();
  },
  house_area:function(){
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'House/house_area',
      data: {
      },
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function (res) {
        if (res.data !== null) {
          
          that.setData({
            area:res.data

          })
          console.log(that.data.area)
        }

      }
    })
  },
  //获取楼盘
  kanfang_xieyi: function () {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'House/kanfang_xieyi',
      data: {
      },
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function (res) {
        if (res.data !== null) {
          // WxParse.wxParse('xieyi', 'html', res.data['content'], that, 0);

          // var desc = that.delHtmlTag(res.data['content'])
          // res.data['content'] = desc;
          that.setData({
            info: res.data

          })
        }

      }
    })
  },
  delHtmlTag: function (msg) {
    var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
    // msg = msg.replace(/[|]*\n/, '') //去除行尾空格
    msg = msg.replace(/&nbsp;/ig, ''); //去掉npsp
    return msg;
  },
  //获取楼盘
  gethouse:function(qu_id){
    var that=this;
    wx.request({
      url: app.globalData.apiUrl + 'House/gethouse',
      data: {
        qu_id: qu_id
      },
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function (res) {
        if (res.data!==null) {
          that.setData({
            house: res.data

          })
        } 
 
      }
    })
  },
  //提交
  formSubmit: function (e) {
    console.log(e)

    var that = this;
    var formData = e.detail.value;
    formData['isRecord'] = 1;
    formData['memberId'] = wx.getStorageSync('userId');
    formData['PayWay'] = 0;
    if (formData['default'] == "请选择" || formData['default'] == ""){
      wx.showToast({
        title: '楼盘必选',
        icon: 'none'
      })
      return;
    }
   
    // if (formData['Code'] == "" || formData['PassWord_2'] == "" || formData['yanzhengma'] == "" || formData['RefereeCode'] == "") {
    //   wx.showToast({
    //     title: '必填',
    //     icon: 'none'
    //   })
    //   return;
    // }
    // that.setData({
    //   button: 'false'
    // })
    // console.log(that.data.button)
    // return ;
    wx.request({
      url: app.globalData.apiUrl + 'House/createOrder',
      data: formData,
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function (res) {
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',

            success: function () {
              setTimeout(function () {
                wx.navigateTo({
                  url: '../baobeiils/baobeiils?IsRecord=1',
                })
              }, 2000);
            }
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
        // that.setData({
        //   button: 'true'
        // })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.alertview = this.selectComponent('#alertview')
  },
  // 按钮点击事件
  clickMe: function() {
    this.setData({
      hiddenAlert: !this.data.hiddenAlert
    })
    // this.alertview.showAlertView();
  },
  // 确定事件
  qdClick: function () {
    this.setData({
      hiddenAlert: !this.data.hiddenAlert
    })
  },
  // confirmEvent: function() {
  //   this.alertview.hideAlertView();
  // },

  // 三联开始
  bindshengChange: function (e) {
    console.log('picker发送选择改变，携带值为省', e.detail.value)
    var shengindex = e.detail.value;
    if (this.data.area[shengindex]['AreaName'] == "请选择") {
      return;
    }
    this.setData({
      shengindex: e.detail.value
    })
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Hotel/getarea',
      data: {
        ParentId: that.data.area[shengindex]['AreaId'],
        type: 'house'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res)
        if (res.data != null) {
          that.setData({
            shiarray: res.data,
          })
        }
      }
    })
  },
  bindShiChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var shiindex = e.detail.value;
    if (this.data.shiarray[shiindex]['AreaName'] == "请选择") {
      return;
    }
    this.setData({
      shiindex: e.detail.value
    })
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Hotel/getarea',
      data: {
        ParentId: that.data.shiarray[shiindex]['AreaId'],
        type: 'hotel'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res)
        if (res.data != null) {
          that.setData({
            quarray: res.data,
          })
        }
      }
    })
  },
  bindQuChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var code = this.data.quarray[e.detail.value]['AreaCode'];
    if (this.data.quarray[e.detail.value]['AreaName'] == "请选择") {
      return;
    }
    this.setData({
      quindex: e.detail.value
    })
    this.gethouse(code)
  },

  // 三联结束
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  lvju: function() {
    wx.navigateTo({
      url: '../lvju/lvju'
    });
  },
  dichan: function() {
    wx.navigateTo({
      url: '../dichan/dichan'
    });
  },
  jiankang: function() {
    wx.navigateTo({
      url: '../jiankang/jiankang'
    });
  },
  news: function() {
    wx.navigateTo({
      url: '../news/news'
    });
  },
})